package com.fxu.framework.biz.mybatis;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.fxu.framework.biz.util.TokenUtil;
import com.fxu.framework.core.base.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/***
 * <p>xfs</p>
 * 字段填充类[创建者，修改者]
 * 其他的创建时间、修改时间、状态、版本都在SQL默认值
 *
 * @author fangxu
 * @version 1.0.0
 ***/
@Slf4j
@Component
public class FieldHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String operator = TokenUtil.getOperator();
        if (Objects.nonNull(operator) && ReflectUtil.getFieldValue(metaObject.getOriginalObject(), Constant.FLD_CREATE_BY) == null) {
            //log.debug("start insert fill ...., create_by={}", operator);
            this.setFieldValByName(Constant.FLD_CREATE_BY, operator, metaObject);
        }
        /*if (metaObject.hasSetter(Constant.FLD_CREATE_TIME)) {
            this.setFieldValByName(Constant.FLD_CREATE_TIME, new Date(), metaObject);
        }*/
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String operator = TokenUtil.getOperator();
        if (Objects.nonNull(operator) && ReflectUtil.getFieldValue(metaObject.getOriginalObject(), Constant.FLD_MODIFY_BY) == null) {
            //log.debug("start update fill ...., update_by={}", operator);
            this.setFieldValByName(Constant.FLD_MODIFY_BY, operator, metaObject);
        }
        /*if (metaObject.hasSetter(Constant.FLD_MODIFY_BY)) {
            this.setFieldValByName(Constant.FLD_MODIFY_BY, new Date(), metaObject);
        }*/
    }
}
